#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "controller_thread.h"
#include <iostream>
using namespace std;


void MainWindow::DisplayStatus(const char attr, const QString &info)
{
    switch (attr) {
    case 0:
        ui->label_robot_index->setText(info);
        break;
    case 1:
        ui->label_status->setText(info);
        break;
    case 2:
        ui->label_left_target->setText(info);
        break;
    case 3:
        ui->label_right_target->setText(info);
        break;
    default:
        break;
    }
}

void MainWindow::OnSlidersValueChangedLeftArm()
{
    QVector<double> data;
    data.push_back(ui->horizontalSlider_x->value());
    data.push_back(ui->horizontalSlider_y->value());
    data.push_back(ui->horizontalSlider_z->value());
    data.push_back(ui->horizontalSlider_rx->value());
    data.push_back(ui->horizontalSlider_ry->value());
    data.push_back(ui->horizontalSlider_rz->value());
    data.push_back(ui->horizontalSlider_gripper->value());
    emit MasterDeviceUpdate(MasterIndex::kLeftDeviceIndex,data);
}
void MainWindow::OnSlidersValueChangedRightArm()
{
    QVector<double> data;
    data.push_back(ui->horizontalSlider_x_2->value());
    data.push_back(ui->horizontalSlider_y_2->value());
    data.push_back(ui->horizontalSlider_z_2->value());
    data.push_back(ui->horizontalSlider_rx_2->value());
    data.push_back(ui->horizontalSlider_ry_2->value());
    data.push_back(ui->horizontalSlider_rz_2->value());
    data.push_back(ui->horizontalSlider_gripper_2->value());
    emit MasterDeviceUpdate(MasterIndex::kRightDeviceIndex,data);
}

void MainWindow::OnSetFreqButton()
{
    emit SetFreq(ui->lineEdit_freq->text().toUInt());
}

void MainWindow::OnConnectSocketButton()
{
    emit ConnectSocket(ui->lineEdit_ip_addr->text(),ui->lineEdit_ip_port->text().toUShort());
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->horizontalSlider_x,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedLeftArm);
    connect(ui->horizontalSlider_y,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedLeftArm);
    connect(ui->horizontalSlider_z,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedLeftArm);
    connect(ui->horizontalSlider_rx,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedLeftArm);
    connect(ui->horizontalSlider_ry,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedLeftArm);
    connect(ui->horizontalSlider_rz,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedLeftArm);
    connect(ui->horizontalSlider_gripper,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedLeftArm);

    connect(ui->horizontalSlider_x_2,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedRightArm);
    connect(ui->horizontalSlider_y_2,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedRightArm);
    connect(ui->horizontalSlider_z_2,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedRightArm);
    connect(ui->horizontalSlider_rx_2,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedRightArm);
    connect(ui->horizontalSlider_ry_2,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedRightArm);
    connect(ui->horizontalSlider_rz_2,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedRightArm);
    connect(ui->horizontalSlider_gripper_2,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChangedRightArm);


    thread_=new ControllerThread(this);
    thread_->start();

    connect(thread_->controller_.get(),&MainController::UpdateStatusInfo,this,&MainWindow::DisplayStatus);

    connect(this->ui->pushButton_freq,&QPushButton::clicked,this,&MainWindow::OnSetFreqButton);
    connect(this->ui->pushButton_socket,&QPushButton::clicked,this,&MainWindow::OnConnectSocketButton);

//    connect(thread_->controller_.get(),&MainController::SendMsgToRobot,this,&MainWindow::DisplayInfo);
    //connect(ui->lineEdit,&QSlider::valueChanged,this,&MainWindow::OnSlidersValueChanged);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete thread_;

}

